import WxRequest from '../assets/plugins/wx-request/lib/index'
class HttpService extends WxRequest {
    constructor(options) {
        super(options)
        this.$$prefix = ''
        this.$$path = {
          wechatSignUp: 'user/signin',
          wechatSignIn: 'wxUser/getWxUser',
          config:       'pay/getByName',
          activity:     'activity/getNewAvtivity',
          image:        'base/deletePic',
          imageAll:      'base/deleteAll',
          order:        'topay/testPay',
          media:        'printerUsername/list',
          print:        '/mail/pic', // 第三方打印接口 
          toPay:        '/toPay/pay'
        }
        this.interceptors.use({
            request(request) {
                let openId     = wx.getStorageSync('openid')
                request.header = request.header || {}
                !request.data && (request.data   = {})
                openId && (request.data.openId    = openId, request.data.openid = openId)
                // request.header['content-type'] = 'application/json'
                if (request.url.indexOf('/Api') !== -1) {
                    let extConfig = wx.getExtConfigSync()
                    // request.header.Authorization = 'Bearer ' + wx.getStorageSync('token')
                    request.header.publicid = extConfig.publicid || __config.publicid
                    request.header.session = wx.getStorageSync('session')
                }
                // wx.showLoading({
                //     title: '加载中', 
                // })
                return request
            },
            requestError(requestError) {
                wx.hideLoading()
                wx.showToast({
                  title: '请求错误',
                  image: '/images/icon-warning.png'
                })
                return Promise.reject(requestError)
            },
            response(response) {
                const { data } = response
                // wx.hideLoading()
                if (response.statusCode === 401) {
                    wx.removeStorageSync('session')
                    wx.redirectTo({
                        url: '/pages/login/index'
                    })
                }

                if (response.statusCode !== 200) {
                  wx.showToast({
                    title: response.data.msg,
                    image: '/images/icon-warning.png'
                  })
                }

                if (response.data.state !== 200) {
                  wx.showToast({
                    title: response.data.msg,
                    image: '/images/icon-warning.png'
                  })
                }

                return response
            },
            responseError(responseError) {
              wx.showToast({
                title: '响应错误',
                image: '/images/icon-warning.png'
              })
              return Promise.reject(responseError)
            },
        })
    }

  /*get请求*/
  /*核心登录*/
  wechatSignIn(params) {
    return this.postRequest(this.$$path.wechatSignIn, {
      data: params
    })
  }
  /*获取活动折扣*/
  getActivity(params) {
    return this.getRequest(this.$$path.activity, {
      data: params
    })
  }
  /*获取基本配置信息*/
  getConfig(params) {
    return this.getRequest(this.$$path.config, {
      data: params
    })
  }
  /*删除图片*/
  deleteImage(params) {
    return this.getRequest(this.$$path.image, {
      data: params
    })
  }
  /*删除所有图片*/
  deleteImageAll(params) {
    return this.postRequest(this.$$path.imageAll, {
      data: params
    })
  }
  /*获取打印设备*/
  getMedia(params) {
    return this.getRequest(this.$$path.media, {
      data: params
    })
  }
  /*post请求*/
  /*发送消息的接口*/
  sendMessage(params) {
    return this.postRequest(this.$$path.sendMessage, {
      data: params
    })
  }
  submitOrder(params) {
    return this.postRequest(this.$$path.order, {
      data: params
    })
  }
  print(params) {
    return this.postRequest(this.$$path.print, {
      data: params
    })
  }
  toPay(params) {
    return this.postRequest(this.$$path.toPay, {
      data: params
    })
  }
}

/*get*/


/*post*/

export default HttpService